<?php

class Category_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }
    function listAll() {
        $this->db->where('category_status', 1);
        $query = $this->db->get('category');
        if($query->num_rows() > 0) {
            return $query->result();
        } else {
            return false;   
        }
    }
    function listSubCate() {
        $this->db->where('sub_category_status', 1);
        $query = $this->db->get('sub_category');
        if($query->num_rows() > 0) {
            return $query->result();
        } else {
            return false;   
        }
    }
    function listSubCateById($id){
        $this->db->order_by("sub_category_id", "ASC");
        $this->db->where('category_id', $id);
        $query = $this->db->get('sub_category');
        if($query->num_rows() > 0) {
            return $query->result();
        } else {
            return false;   
        }
    }
     /*
     * get info
     */
     function getInfo($id) {
        $this->db->where('category_id', $id);
        $query = $this->db->get('category');
        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return false;
        }
    }
    function getInfoByLink($link) {
        $this->db->where('category_links', $link);
        $query = $this->db->get('category');
        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return false;
        }
    }
    function getInfoByClass($class) {
        $this->db->where('class', $class);
        $query = $this->db->get('category');
        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return false;
        }
    }
    function related_cate($category_id){
        $category = $this->getInfo($category_id);
        $this->db->order_by("category_id", "ASC");
        $this->db->where('category_group', $category->category_group);
        $this->db->where('category_id <>', $category_id);
        $this->db->limit(3,0);
        $query = $this->db->get('category');
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return false;
        }
    }
    function getSubInfo($id) {
        $this->db->where('sub_category_id', $id);
        $query = $this->db->get('sub_category');
        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return false;
        }
    }
    function getSubInfoByLink($link) {
        $this->db->where('sub_category_links', $link);
        $query = $this->db->get('sub_category');
        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return false;
        }
    }
}
